﻿using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using SoarDEditor.Pandora;
using Spine.Unity;

public class CustomMenu
{
    #region 菜单
    [MenuItem("Custom/生成密钥", false, 1)]
    static void Me_GenerateKey()
    {
        ConfigHelper.GenerateKey();
    }

    [MenuItem("Custom/重置版本", false, 1)]
    static void Me_ResetVersion()
    {
        ConfigHelper.Version = ConfigData.DEFAULT_VERSION;
    }

    [MenuItem("Custom/AB/导出AB", false, 150)]
    static void ExportAB()
    {
        ExportProject.ExportAB();
    }

    [MenuItem("Custom/AB/输出AB", false, 150)]
    static void PrintAB()
    {
        CheckAssetBundle.PrintAssetBundle2();
    }

    [MenuItem("Custom/AB/检查交叉依赖", false, 150)]
    static void CheckCrossDependency()
    {
        CheckAssetBundle.CheckCrossDependency();
    }
    #endregion

    #region UI
    [MenuItem("GameObject/UI/Image")]
    static void CreateSDImage()
    {
        SDImage img = CreateObj<SDImage>("Image", Selection.activeTransform);
        img.raycastTarget = false;
    }

    [MenuItem("GameObject/UI/EmptyRaycast")]
    static void CreateEmptyRaycast()
    {
        EmptyRaycast emptyRay = CreateObj<EmptyRaycast>("EmptyRaycast", Selection.activeTransform);
        emptyRay.raycastTarget = true;
    }

    [MenuItem("GameObject/UI/Text")]
    static void CreateSDText()
    {
        SoarDText text = CreateObj<SoarDText>("Text", Selection.activeTransform);
        text.fontSize = 20;
        text.rectTransform.sizeDelta = new Vector2(100, 30);
        text.raycastTarget = false;
    }

    static T CreateObj<T>(string name, Transform parent) where T : Component
    {
        GameObject obj = new GameObject();
        T component = obj.AddComponent<T>();
        obj.name = name;
        obj.transform.parent = parent;
        obj.transform.localScale = Vector3.one;
        obj.transform.localPosition = Vector3.zero;
        return component;
    }
    #endregion

    #region 图集
    [MenuItem("Assets/打压缩图集", false, 100)]
    static void ExportCompressAtlas()
    {
        PdrEditorHelper.ExcuteAssetOperat(obj =>
        {
            bool isCompress = true;
            bool isSplitChannel = true;
            AtlasMaker.MakeAtlas(obj, isCompress, isSplitChannel);
        }, SelectionMode.Assets);
    }
    #endregion

    [MenuItem("GameObject/导出层级", false, 20)]
    static void Assets_ExportGameObjectHierarchy()
    {
        ExportPanelHierarchy.ExportSelect(Selection.activeObject);
        Debug.Log("Export GameObject Hierarchy Completed");
    }

    private const string importSpinePathRecordKey = "IMPORT_SPINE_PATH_INPUT";

    [MenuItem("Assets/Import Package/Import Spine", false, 100)]
    private static void ImportSpine()
    {
        Object selectObj = Selection.GetFiltered(typeof(Object), SelectionMode.Assets)[0];
        if (selectObj != null)
        {
            string targetPath = AssetDatabase.GetAssetPath(selectObj);
            if (!string.IsNullOrEmpty(targetPath))
            {
                string defaultPath = EditorPrefs.GetString(importSpinePathRecordKey, "D:\\");
                string inputPath = EditorUtility.OpenFolderPanel("Open Folder Dialog", defaultPath, "spine");
                if (!string.IsNullOrEmpty(inputPath))
                {
                    string savePath = inputPath.Substring(0, inputPath.LastIndexOf("/"));
                    EditorPrefs.SetString(importSpinePathRecordKey, savePath);

                    DirectoryInfo inputDirInfo = new DirectoryInfo(inputPath);
                    targetPath += "/" + inputDirInfo.Name;
                    if (!Directory.Exists(targetPath))
                        Directory.CreateDirectory(targetPath);

                    string atlasPath = null;//途径路径
                    string dataPath = null;//数据路径
                    string assetPath = null;//资源路径
                    var fileInfos = inputDirInfo.GetFiles();
                    foreach (var info in fileInfos)
                    {
                        string path;
                        if (info.Extension == ".skel")
                        {
                            path = targetPath + "/" + info.Name + ".bytes";
                            dataPath = path;
                        }
                        else if (info.Extension == ".json")
                        {
                            path = targetPath + "/" + info.Name + ".txt";
                            dataPath = path;
                        }
                        else if (info.Extension == ".atlas")
                        {
                            path = targetPath + "/" + info.Name + ".txt";
                            atlasPath = path.Replace(".atlas.txt", "_Atlas.asset");
                        }
                        else
                        {
                            path = targetPath + "/" + info.Name;
                            assetPath = targetPath + "/" + Path.GetFileNameWithoutExtension(info.Name) + ".asset";
                        }
                        File.Copy(info.FullName, path, true);
                    }
                    AssetDatabase.Refresh();
                    //生产动画数据文件
                    if (!string.IsNullOrEmpty(dataPath) && !string.IsNullOrEmpty(atlasPath))
                    {
                        TextAsset spineJson = AssetDatabase.LoadAssetAtPath<TextAsset>(dataPath);
                        AtlasAssetBase atalsAsset = AssetDatabase.LoadAssetAtPath<AtlasAssetBase>(atlasPath);
                        if (spineJson && atalsAsset)
                        {
                            SkeletonDataAsset skeletonDataAsset = ScriptableObject.CreateInstance<SkeletonDataAsset>();
                            skeletonDataAsset.atlasAssets = new AtlasAssetBase[1] { atalsAsset };
                            skeletonDataAsset.skeletonJSON = spineJson;

                            AssetDatabase.CreateAsset(skeletonDataAsset, assetPath);
                            AssetDatabase.SaveAssets();
                        }
                    }
                }
            }
        }
    }
}